package environment;

import java.util.ArrayList;

import java.awt.*;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;

import settings.Settings;

public class UI extends JFrame {
	
	JPanel panel = new JPanel();
	
	int side;
	int cellCount;
	
	// new colors
	Color dGreen = new Color( 0, 128, 0 );
	Color dBlue = new Color( 0, 255, 255 );
	Color gold = new Color( 255, 215, 0 );
	Color brick = new Color( 158, 102, 31 );
	
	// array of GUI panels
	ArrayList < JPanel > reference;
	
	public UI() {
				
		this.side = Settings.side;
		this.cellCount = Settings.cellCount;
		
		reference = new ArrayList < JPanel > ();
		
		initUI();
	}
	
	public void initUI() {
		
		// setting up GUI layout
		panel.setBackground( Color.DARK_GRAY );
		panel.setBorder( BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) );
		panel.setLayout( new GridLayout( side, side, 5, 5 ) );
		
		for( int i = 0; i < cellCount; i++ ) {
			
			// add panels to layout
			JPanel hatchling = new JPanel( new BorderLayout() );
			//hatchling.setBackground( Color.BLACK );
			reference.add( hatchling );
			panel.add( reference.get( i ) );
		}
		
		add( panel );
		
		// set panel size
		setTitle( "Nexus" );
		setSize( 750, 750 );
		setLocationRelativeTo( null );
		setDefaultCloseOperation( EXIT_ON_CLOSE );
	}
	
	// method for changing GUI panel colors
	public void setCellColor( int colorCode, int current ) {
		
		switch( colorCode ) {
		
		case 0: // agent
			reference.get( current ).setBackground( Color.RED );
			break;
			
		case 1: // grass
			reference.get( current ).setBackground( Color.GREEN );
			break;
			
		case 2: // water
			reference.get( current ).setBackground( Color.BLUE );
			break;
			
		case 3: // road
			reference.get( current ).setBackground( Color.GRAY );
			break;
			
		case 4: // forest
			reference.get( current ).setBackground( dGreen );
			break;
			
		case 5: // deep water
			reference.get( current ).setBackground( dBlue );
			break;
			
		case 6: // rock
			reference.get( current ).setBackground( brick );
			break;
			
		case 7: // goal
		case 8: // start
			reference.get( current ).setBackground( gold );
			break;
			
		default:
			break;
		}
	}
	
	// load initial map data for Environment class
	public void mapLoad( int [] loadingData ) {
		
		int current = 0;
		
		for( int i = 0; i < side; i++ ) {
			for( int j = 0; j < side; j++ ) {
				
				setCellColor( loadingData[ current ], current );
				current++;
			}
		}
	}
		
	// update map after agents make moves
	public void moveUpdate( int currLoc, int currLocColor, int nextLoc, int nextLocColor ) {
		
		setCellColor( currLocColor, currLoc );
		setCellColor( nextLocColor, nextLoc );
	}
	
	// update single cells for whatever reason
	public void cellUpdate( int cellLoc, int newColor ) {
		
		setCellColor( newColor, cellLoc );
	}
	
}